package com.ruoyi.module.cyl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.YhBaseinfo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IYhBaseinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 人员基础信息Controller
 *
 * @author yinhe
 * @date 2022-11-07
 */
@RestController
@RequestMapping("/cyl/baseinfo")
public class CylBaseinfoController extends BaseController
{
    @Autowired
    private IYhBaseinfoService yhBaseinfoService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询人员基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('cyl:baseinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhBaseinfo yhBaseinfo)
    {
        startPage();
        List<YhBaseinfo> list = yhBaseinfoService.selectYhBaseinfoList(yhBaseinfo);
        return getDataTable(list);
    }

    /**
     * 导出人员基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('cyl:baseinfo:export')")
    @Log(title = "人员基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhBaseinfo yhBaseinfo)
    {
        List<YhBaseinfo> list = yhBaseinfoService.selectYhBaseinfoList(yhBaseinfo);
        ExcelUtil<YhBaseinfo> util = new ExcelUtil<YhBaseinfo>(YhBaseinfo.class);
        util.exportExcel(response, list, "人员基础信息数据");
    }

    /**
     * 获取人员基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('cyl:baseinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhBaseinfoService.selectYhBaseinfoById(id));
    }

    /**
     * 新增人员基础信息
     */
    @PreAuthorize("@ss.hasPermi('cyl:baseinfo:add')")
    @Log(title = "人员基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhBaseinfo yhBaseinfo)
    {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhBaseinfo.setUserId(sysUser.getUserId());
        yhBaseinfo.setDeptId(sysUser.getDeptId());
        yhBaseinfo.setWorkPlace(String.valueOf(sysUser.getDeptId()));
        return toAjax(yhBaseinfoService.insertYhBaseinfo(yhBaseinfo));
        /*return toAjax(yhBaseinfoService.insertYhBaseinfo(yhBaseinfo));*/
    }

    /**
     * 修改人员基础信息
     */
    @PreAuthorize("@ss.hasPermi('cyl:baseinfo:edit')")
    @Log(title = "人员基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhBaseinfo yhBaseinfo,SysUser user)
    {
            String gh = yhBaseinfo.getUserName();
            String xm = yhBaseinfo.getName();
            List<SysUser> list = userService.selectUserList(user);

        for (int i = 0; i < list.size(); i++) {
            if (StringUtils.isNumeric(list.get(i).getUserName())){
            int u1 = Integer.valueOf(list.get(i).getUserName().trim()).intValue();
            int u2 =Integer.valueOf(gh.trim()).intValue();
            if (u1==u2){
                SysUser sysUser1 = userService.selectUserByUserName(list.get(i).getUserName());
                sysUser1.setNickName(xm);
                userService.updateUserProfile(sysUser1);
            }
            }
        }
        return toAjax(yhBaseinfoService.updateYhBaseinfo(yhBaseinfo));
    }

    /**
     * 删除人员基础信息
     */
    @PreAuthorize("@ss.hasPermi('cyl:baseinfo:remove')")
    @Log(title = "人员基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhBaseinfoService.deleteYhBaseinfoByIds(ids));
    }
}
